<?php
/**
 * @file
 * All admin related stuff
 */

/**
 * saves changes made by the callback and
 * displays new revision
 */
function race_admin($nid) {
  $node = node_load($nid);
  return _race_admin($node);
}

function _race_admin($node) {
  
  $node->content['admin_form'] = array(
    '#value' => _get_admin_form($node),
    '#weight' => 0
   );
    
  $node->content['signups'] = array(
    '#value' => _get_signupslist_for_admin($node),
    '#weight' => 1
  );
  
  $node->content['assignments'] = array(
    '#value' => _get_assignedlist_for_admin($node),
    '#weight' => 2
  );
  
  $node->content['unassigned'] = array(
    '#value' => _get_unassignedlist_for_admin($node),
    '#weight' => 3
  );
  
  return drupal_render($node->content);
}


function race_overview($nid) {
  $node = node_load($nid);
  $assignments = _get_assignments($node);
  $node->content['overview'] = array(
    '#value' => _xsl_assignmentlist_overview($assignments),
    '#weight' => 0
  );

  return drupal_render($node->content);
}

function race_mail($nid) {
  $node = node_load($nid);
  $assignments = _get_assignments($node);

  $node->content['mail'] = array(
    '#value' => _xsl_assignmentlist_mail($assignments),
    '#weight' => 0
  );
  
  return drupal_render($node->content);
}

function _get_signupslist_for_admin($node) {
  $signups = _get_signups($node);
  $title = "<h3>".t("Sign up list")."</h3>";
  return $title._xsl_signuplist_user_for_admin($signups);
}

function _get_unassignedlist_for_admin($node) {
  $assignments = _get_assignments($node);
  $signups = _get_signups($node);
  $config = _get_config($node);
  $unassigned = _get_unassigned_users($signups, $assignments);
  if (sizeof($unassigned) > 0) {
    $html = "<h3>".t("Unassigned")."</h3>";
    $html .= "<table>";
    $html .= "<tr><th>User</th><th>Boats with free places</th></tr>";
    foreach ($unassigned as $user) {
      $html .= "<td>".$user->name."</td>";
      $html .= "<td>";
      foreach (_get_boats_with_free_places($config, $assignments) as $boat) {
        $html .= _get_assignment_link($node->nid, $user->name, (String)$boat['id']);
	    $html .= " / ";
	  }
	  $html .= _get_rejection_link($node->nid, $user->name);
      $html .= "</td></tr>";
    }
    $html .= "</table>";
    return $html;
  }
}

function _get_assignedlist_for_admin($node) {
  $assignments = _get_assignments($node);
  $signups = _get_signups($node);
  $config = _get_config($node);
  $boats = _get_boats($config);
  
  if (sizeof($boats) > 0) {
    $html = "<h3>".t("Assigned")."</h3>";
    $html .= "<table>";
    $html .= "<tr><th>Boat</th><th>Crew</th></tr>";
    $html .= "<tr>";
    foreach ($boats as $boat) {
      $html .= "<td valign='top'>".$boat['name']."</td>";
      $html .= "<td valign='top'>";
      foreach (_get_assignments_by_boatid($assignments, (String) $boat['id']) as $signup) {
        $html .= $signup['user'];
        $html .= " (";
        foreach (_get_boats_with_free_places($config, $assignments) as $freeBoat) {
          if ((String)$freeBoat["id"] != (String)$boat["id"] ) {
		      $html .= _get_assignment_link($node->nid, (String)$signup['user'], (String)$freeBoat['id']);
	          $html .= ", ";
          }
        }
        $html .= _get_unassignment_link($node->nid, (String)$signup['user'], (String)$boat['id']);
        $html .= ")<br />";
      }
      $html .= "</td></tr>";
    }
    $html .= "</table>";
    return $html;
  }
}

function _get_admin_form($node) {
  $nid = $node->nid;
  $separator = "&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;";
  $html = "<h3>".t("Admin")."</h3>";
  $html .= "<p>";
  $html .= "<a href='".base_path()."node/$nid'>Node</a>".$separator;
  $html .= "<a href='".base_path()."race/automatic_assign/$nid'>Automatic assign</a>$separator";
  $html .= "<a href='".base_path()."race/change_mode/$nid'>Change mode</a>$separator";
  $html .= "<a href='".base_path()."race/reset/$nid'>Reset</a>$separator";
  $html .= "<a href='".base_path()."race/$nid/mail'>Mail</a>$separator";
  $html .= "<a href='".base_path()."race/$nid/overview'>Overview</a>";
  $html .= "</p>";
  return $html;
}

function _xsl_signuplist_user_for_admin(SimpleXMLElement $signups) { 
  $file = drupal_get_path('module', "race").DIRECTORY_SEPARATOR."xsl".DIRECTORY_SEPARATOR."admin".DIRECTORY_SEPARATOR."signuplist.xml";
  $xsl = new DOMDocument();
  $xsl->load($file);
  return _xsl($signups->asXML(), $xsl);
}

function _xsl_assignmentlist_mail(SimpleXMLElement $assignments) { 
  if (sizeof($assignments) > 0) {
	  $file = drupal_get_path('module', "race").DIRECTORY_SEPARATOR."xsl".DIRECTORY_SEPARATOR."mail.xml";
	  $xsl = new DOMDocument();
	  $xsl->load($file);
	  return _xsl($assignments->asXML(), $xsl);
  } else {
    return "no assignments";
  }
}

function _xsl_assignmentlist_overview(SimpleXMLElement $assignments) { 
  if (sizeof($assignments) > 0) {
	$file = drupal_get_path('module', "race").DIRECTORY_SEPARATOR."xsl".DIRECTORY_SEPARATOR."overview.xml";
	$xsl = new DOMDocument();
	$xsl->load($file);
	return _xsl($assignments->asXML(), $xsl);
  } else {
    return "no assignments";
  }
}

function _get_assignment_link($nid, $userName, $boatId) {
  return "<a href='".base_path()."race/assign/".$nid."/".$userName."/".$boatId."'>".$boatId."</a>";
}

function _get_unassignment_link($nid, $userName, $boatId) {
  if ($nid!=null && $userName!=null && $boatId!=null) {
    return "<a href='".base_path()."race/unassign/".$nid."/".$userName."/".$boatId."'>".t("unassign")."</a>";
  } else {
    return "";
  }
}

function _get_rejection_link($nid, $userName) {
  if ($nid!=null && $userName!=null) {
  return "<a href='".base_path()."race/reject/".$nid."/".$userName."'>".t("reject")."</a>";
  } else {
    return "";
  }
}

